<?php ob_start();
include("header.php" );
///////Facebook Login //////////////
require 'facebook/facebook.php';
$facebook = new Facebook(array(
  'appId' => '363494293683738',
  'secret' => 'f9a6ca49f09ff6a5ee6fa0963fbba676',
));
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me');	
  } catch (FacebookApiException $e) {
    error_log($e);
    $user = null;
  }
}
//print_r($user_profile);
///Check if login with facebook
if(isset($user_profile)&&(!isset($_SESSION['id']))){
	$chk=mysql_query("SELECT * FROM `my_user` WHERE `facebook_id`='".$user_profile['id']."' ")or die(mysql_error());	
	if(!mysql_num_rows($chk)){
		$sql=mysql_query("INSERT INTO `my_user` SET `facebook_id`='".$user_profile['id']."', 
													`fname`='".$user_profile['first_name']."',
													`lname`='".$user_profile['last_name']."',													
													`dob`='".addslashes($user_profile['birthday'])."',
													`gender`='".$user_profile['gender']."' ")or die(mysql_error());
		$id=mysql_insert_id();
		if($sql){ 
			$sql2=mysql_query("SELECT * FROM `my_user` WHERE `id`=$id ")or die(mysql_error());	
			$rows=mysql_fetch_array($sql2);
			$_SESSION['id']=	$rows['id'];
			$_SESSION['fname']=	$rows['fname'];
			//header("location:myaccount.php"); exit;
		}
	}else{
		$sql2=mysql_query("SELECT * FROM `my_user` WHERE `facebook_id`='".$user_profile['id']."' ")or die(mysql_error());	
		$rows=mysql_fetch_array($sql2);
		$_SESSION['id']=	$rows['id'];
		$_SESSION['fname']=	$rows['fname'];
		//header("location:myaccount.php"); exit;
	}	
}
//////End Facebook Login///////////////

if(isset($_SESSION['url_id'])&&($_SESSION['url_id']!="")){
	header("location:create_page_step1.php"); exit;
}else{header("location:myfan_page.php"); exit;}
?>